package com.xiaoxu.spzx.manager.service.impl;

import com.xiaoxu.spzx.log.annotation.Log;
import com.xiaoxu.spzx.manager.mapper.SysRoleMenuMapper;
import com.xiaoxu.spzx.manager.service.SysMenuService;
import com.xiaoxu.spzx.manager.service.SysRoleMenuService;
import com.xiaoxu.spzx.model.dto.system.AssginMenuDto;
import com.xiaoxu.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        //sysMenuList是所有的菜单列表 //查询了所有菜单集合
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        //根据角色id查询角色所拥有的菜单id
        List<Long> roleMenuIds =  sysRoleMenuMapper.findRoleMenuByRoleId(roleId);

        Map<String,Object> map = new HashMap<>();
        map.put("sysMenuList",sysMenuList);
        map.put("roleMenuIds",roleMenuIds);

        return  map;
    }
    @Log(title = "角色菜单模块" , businessType = 2 )
    @Transactional
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {


        //1首先删除分配的菜单id  //据角色的id删除其所对应的菜单数据
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());

        //2.保存角色 id  和 菜单已经✔的id

        List<Map<String, Number>> menuInfo = assginMenuDto.getMenuIdList();

        if (menuInfo != null && menuInfo.size() > 0) {

            sysRoleMenuMapper.doAssign(assginMenuDto);
        }

    }
}
